package cn.stevenyang.tests;

import cn.stevenyang.BasicInterpreter;
import cn.stevenyang.gen.BasicParserLexer;
import cn.stevenyang.gen.BasicParserParser;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CommonTokenStream;
import org.junit.jupiter.api.Test;

public class ControlFlowTest {
    @Test
    public void simpleFunctionTest() {
        BasicInterpreter interpreter = runCode("Print(\"Testing...\")\r\nPrint(\"The devil appears!! \")");
    }
    @Test
    public void loopTest() {
        BasicInterpreter interpreter = runCode("For i = 1 To 10 Step 2 \r\n Print(\"The devil appears!! #\" & CStr(i)) \r\nNext");
    }
    @Test
    public void loopTest2() {
        BasicInterpreter interpreter = runCode("For i = 10 DownTo 1 Step 2 \r\n Print(\"The devil appears!! #\" & CStr(i)) \r\n Print(\"*******************\") \r\nNext");
    }
    @Test
    public void loopWithConditionBreakAndContinue() {
        BasicInterpreter interpreter = runCode("For i = 1 To 100 \r\n If i == 17 Then \r\n Break \r\n ElseIf i == 13 Then \r\n Continue \r\n ElseIf i < 5 Then \r\n Print(\"Larr~~~\") \r\n Else \r\n Print(\"The devil appears!! #\" & CStr(i)) Print(\"*******************\") \r\n End If \r\nNext \r\n Print(\"Thank you!\")");
    }
    @Test
    public void whileTest() {
        BasicInterpreter interpreter = runCode("Dim i As Integer = 1 \r\n While i <= 10\r\n Print(\"排序中\" & CStr(i)) \r\n i = i + 1 \r\n Print(\"----===------\") \r\nEnd While");
    }
    public BasicInterpreter runCode(String code) {
        BasicParserLexer lexer = new BasicParserLexer(CharStreams.fromString(code));
        CommonTokenStream tokenStream = new CommonTokenStream(lexer);
        BasicParserParser parser = new BasicParserParser(tokenStream);

        BasicParserParser.ProcessContext tree = parser.process();
        BasicInterpreter interpreter = new BasicInterpreter();
        interpreter.visit(tree);
        return interpreter;
    }
}
